package com.wing.cabinet.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.Cabinet;
import com.wing.cabinet.model.entity.CellInventorySpace;
import com.wing.cabinet.model.entity.CellSingleInventoryProduct;
import com.wing.cabinet.model.request.CellInventorySpaceForm;
import com.wing.cabinet.model.response.CellInventoryProductVO;
import com.wing.cabinet.model.response.CellInventorySpaceVO;
import com.wing.cabinet.model.response.CellSingleInventoryProductVO;
import com.wing.cabinet.service.CellInventorySpaceService;
import com.wing.cabinet.service.CellSingleInventoryProductService;
import com.wing.cabinet.utils.ExcelUtils;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-售刀柜库存管理(按主柜)", tags = "#/cabinet/cellInventoryCabinet")
@RestController("cellInventoryCabinetController")
@RequestMapping("cabinet/admin/cellInventoryCabinet")
@RequiredArgsConstructor
public class CellInventoryCabinetController {

    private final CellSingleInventoryProductService cellSingleInventoryProductService;

    @ApiOperation(value = "查询所有售刀柜产品库存(按主柜)", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<CellSingleInventoryProductVO> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String code
            , String cabCode
            , String skuCode
            , String skuCode4Detail
            , String spaceCode
            , String mainCabCode
            , String queryFlag
            , Cabinet.CabinetNature cabinetNature
            ,String oldSpecCode
            ,String belongCabinet
            ,String query
            ){

        Page<CellInventorySpace> pg = new Page<>(page, limit);
        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("code",code);
        queryParam.put("cabCode",cabCode);
        queryParam.put("skuCode",skuCode);
        queryParam.put("skuCode4Detail",skuCode4Detail);
        queryParam.put("spaceCode",spaceCode);
        queryParam.put("mainCabCode",mainCabCode);
        queryParam.put("queryFlag",queryFlag);
        queryParam.put("enterpriseCode", JwtUtils.getEnterpriseCode());
        if (cabinetNature!=null){
            queryParam.put("cabinetNature",cabinetNature.getValue());
        }
        queryParam.put("oldSpecCode",oldSpecCode);
        queryParam.put("belongCabinet",belongCabinet);
        if (StringUtils.isNotBlank(query)){
            queryParam.put("query",query);
        }
        //搜索

        IPage<CellSingleInventoryProductVO> list = cellSingleInventoryProductService.pageListPressCabinet(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "导出按货道库存数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response, String enterpriseCode){
        List<CellSingleInventoryProductVO> cellInventorySpaceVOList = new ArrayList<>();
        Map paramsMap = new HashMap();
        //paramsMap.put("enterpriseCode", JwtUtils.getEnterpriseCode());
        cellInventorySpaceVOList = cellSingleInventoryProductService.exportPressCabinet();
        ExcelUtils.export(response,"库存列表（按主柜）",cellInventorySpaceVOList, CellInventoryProductVO.class);
    }


    @ApiOperation(value = "查询售刀柜产品库存分布", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/getListInventoryBySkuCode")
    public PageResult<CellSingleInventoryProductVO> getInventoryBySkuCodeAndBelongCabinet(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String skuCode
            , String belongCabinet
    ){
        Page<CellSingleInventoryProduct> pg = new Page<>(page, limit);
        Map queryParam = new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("skuCode",skuCode);
        queryParam.put("belongCabinet",belongCabinet);
        //搜索
        IPage<CellSingleInventoryProductVO> list = cellSingleInventoryProductService.getInventoryBySkuCodeAndBelongCabinet(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }
}
